import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xvideos/hj_model/cartoon_media_info.dart';
import 'package:xvideos/hj_model/home/home_tabs_entity.dart';
import 'package:xvideos/hj_page/cartoon_business/acg_widget_item.dart';
import 'package:xvideos/hj_page/cartoon_business/search_tuji_cell.dart';
import 'package:xvideos/hj_page/home_business/home_cell_style/video_simple_cell_widget.dart';
import 'package:xvideos/hj_page/home_business/home_sub_module/widget/special_topics_view.dart';
import 'package:xvideos/hj_utils/sliver_delegate.dart';
import 'package:xvideos/hj_utils/widget_utils.dart';
import 'package:xvideos/tools_base/banner/ads_grid_view_widget.dart';
import 'package:xvideos/tools_base/loading/l_error_widget.dart';
import 'package:xvideos/tools_base/loading/loading_center_widget.dart';
import 'package:xvideos/tools_base/refresh/pull_refresh.dart';

import '../home_business/home_cell_style/home_jg_area_widget.dart';
import 'cartoon_sub_grid_logic.dart';
import 'cartoon_sub_module_page.dart';

//海角样式
class CartoonSubGridPage extends StatefulWidget {
  final ModuleData? tagData; //模块model
  final CommunityType type; //-1:动漫和漫画 0:图集 1:黄游 2:小说
  const CartoonSubGridPage({super.key, this.tagData, this.type = CommunityType.Pic});

  @override
  State<CartoonSubGridPage> createState() => _CartoonSubGridPageState();
}

class _CartoonSubGridPageState extends State<CartoonSubGridPage> {
  late CartoonSubGridLogic logic;
  late String tag;

  @override
  void initState() {
    super.initState();
    tag = '${widget.tagData?.id ?? ''}${widget.type}';
    logic = Get.put(
      CartoonSubGridLogic(tagData: widget.tagData, type: widget.type),
      tag: tag,
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<CartoonSubGridLogic>(
      tag: tag,
      builder: (_) => pullYsRefresh(
        onInit: (ctr) => _.refreshCtr = ctr,
        onRefresh: (ctr) => _.loadData(),
        onLoading: (ctr) => _.loadMoreData(),
        child: _buildContent(),
      ),
    );
  }

  _buildContent() {
    return CustomScrollView(
      slivers: [
        SliverToBoxAdapter(child: 12.sizeBoxH),
        SliverToBoxAdapter(
          child: AdsGridViewWidget(
            widget.type == CommunityType.Cartoon ? 4 : 7,
            isBigStyle: true,
            padding: EdgeInsets.fromLTRB(16, 0, 16, 12),
          ),
        ),
        if(widget.type == CommunityType.Cartoon)
          SliverToBoxAdapter(child: HomeJgAreaWidget(widget.tagData)),
        if (logic.type == CommunityType.Cartoon)
          SliverToBoxAdapter(
            child: SpecialTopicsView(
              logic.specials,
              moudle: logic.tagData,
              padding: EdgeInsets.fromLTRB(12, 12, 12, 12),
              isACG: true,
            ),
          ),
        _buildSortType(),
        _buildGridList(),
      ],
    );
  }

  //排序
  _buildSortType() {
    return SliverPersistentHeader(
      pinned: true,
      floating: false,
      delegate: MySliverDelegate(
        maxHeight: 36,
        minHeight: 36,
        child: Container(
          alignment: Alignment.center,
          color: Color(0xff030F18),
          child: TabBar(
              tabAlignment: TabAlignment.center,
              controller: logic.tabCtr,
              isScrollable: true,
              tabs: List.generate(
                logic.sortType.length,
                (index) => Padding(
                  padding: const EdgeInsets.only(top: 12, bottom: 4),
                  child: Text(logic.sortType[index]),
                ),
              ),
              onTap: (index) => logic.onChangeIndexAction(index),
              labelColor: Colors.white.withOpacity(.9),
              labelStyle: const TextStyle(fontSize: 14, fontWeight: FontWeight.w400),
              unselectedLabelColor: Colors.white.withOpacity(0.45),
              unselectedLabelStyle: const TextStyle(fontSize: 14, fontWeight: FontWeight.w400),
              indicator: BoxDecoration()),
        ),
      ),
    );
  }

  _buildGridList() {
    if (logic.dataList == null)
      return SliverToBoxAdapter(
          child: SizedBox(
        height: 400,
        child: LoadingCenterWidget(),
      ));
    if (logic.dataList!.isEmpty)
      return SliverToBoxAdapter(
          child: SizedBox(
        height: 400,
        child: CErrorWidget(retryOnTap: () => logic.loadData()),
      ));
    return SliverPadding(
      padding: EdgeInsets.fromLTRB(16, 12, 16, 12),
      sliver: SliverGrid(
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: logic.crossAxisCount,
          mainAxisSpacing: 12,
          crossAxisSpacing: 5,
          childAspectRatio: logic.childAspectRatio,
        ),
        delegate: SliverChildBuilderDelegate(
          (context, index) {
            //-1动漫/漫画 0-图集 1-黄游 2-小说
            if (logic.type == CommunityType.Cartoon) {
              CartoonMediaInfo meidiaInfo = logic.dataList?[index];
              return AcgItemWidget(info: meidiaInfo, coverV: true);
            } else if (logic.type == CommunityType.Pic) {
              return SearchTujiCell(
                videoModel: logic.dataList?[index],
                textline: 1,
              );
            } else if (logic.type == CommunityType.Game) {
              return VideoSimpleCellWidget(
                videoModel: logic.dataList?[index],
                isFromHY: true,
                textLines: 1,
              );
            } else if (logic.type == CommunityType.Novel) {
              CartoonMediaInfo meidiaInfo = logic.dataList?[index];
              return AcgItemWidget(info: meidiaInfo, coverV: true);
            }
            return Container();
          },
          childCount: logic.dataList?.length ?? 0,
        ),
      ),
    );
  }
}
